<?php
/**
 * User: Drery
 * Date: 2015/12/2
 * Time: 20:10
 */

namespace backend\models;


use yii\db\ActiveRecord;

class Permission extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%admin_permission}}';
    }

    public function rules()
    {
        return [
            [['description','link'],'required'],
            [['level','pid','font'],'safe'],
            ['arrangement','default','value'=>0]
        ];
    }

    public function attributeLabels()
    {
        return [
            'description' => '权限名',
            'pid' => '所属权限',
            'arrangement' => '序号',
            'link' => '对应链接'
        ];
    }

    public function getChildren()
    {
        return $this->hasMany(Permission::className(),['pid'=>'id'])->where(['is_deleted'=>0])->orderBy(['arrangement'=>SORT_ASC]);
    }

    public function getParent()
    {
        return $this->hasOne(Permission::className(), ['id' => 'pid'])->where(['is_deleted'=>0]);
    }

    public static function getNodeList()
    {
        return self::find()->where(['pid'=>0,'is_deleted'=>0])->with('children')->orderBy(['arrangement'=>SORT_ASC])->all();
    }
}